package com.example.finalwork;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.finalwork.DAO.UserDAO;
import com.example.finalwork.lmpl.UserDAOlmpl;
import com.example.finalwork.Entity.User;

import java.util.List;

import static org.junit.Assert.assertNotEquals;

public class ChangePdActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "ChangePdActivity";
    private Button ConfirmChange_button,Exit2_button;
    private EditText Oldpassword,NewPassword,ConfirmPassword,UserNName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changer);
        findViewById(R.id.ConfirmChange_button).setOnClickListener(this);
        findViewById(R.id.Exit2_button).setOnClickListener(this);
        Oldpassword = findViewById(R.id.oldpassword);
        NewPassword = findViewById(R.id.NewPassword);
        ConfirmPassword = findViewById(R.id.ConfirmPassword);
        UserNName = findViewById(R.id.UserNName);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ConfirmChange_button:
                if(Oldpassword.getText().toString().equals(NewPassword.getText().toString()))
                    Toast.makeText(this,"新密码不能与旧密码相同",Toast.LENGTH_SHORT).show();
                else if(!NewPassword.getText().toString().equals(ConfirmPassword.getText().toString()))
                    Toast.makeText(this,"新密码与确认密码不相同",Toast.LENGTH_SHORT).show();
                else
                {
                    UserDAO userDAO = new UserDAOlmpl(this);
                    List<User> users = userDAO.GetAllUser();
                    for(int i = 0 ; i < users.size() ; i++)
                    {
                        User olduser = users.get(i);
                        //Log.d(TAG,olduser.getUserName()+"..."+UserNName.getText().toString());
                        if(olduser.getUserName().equals(UserNName.getText().toString()))
                        {
                            if(!olduser.getPassWord().equals(Oldpassword.getText().toString()))
                            {
                                Toast.makeText(this,"旧密码不正确",Toast.LENGTH_SHORT).show();
                                Oldpassword.requestFocus();
                                return;
                            }
                            User user = new User();
                            user.setUserName(UserNName.getText().toString());
                            user.setPassWord(NewPassword.getText().toString());
                            int result = userDAO.updateUser(user);
                            Log.d(TAG,"add user result - >" + result);
                            assertNotEquals(-1,result);
                            Toast.makeText(this,"修改成功，回到上一级！",Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(this, LoginActivity.class));
                            return;
                        }
                    }
                    Toast.makeText(this,"用户名不存在，请重新输入！",Toast.LENGTH_SHORT).show();
                    UserNName.requestFocus();
                }
                break;
            case R.id.Exit2_button:
                Toast.makeText(this,"返回上一级",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, LoginActivity.class));
                break;

        }
    }
}
